package be.fomp.jeve.model.data.standard.jumps;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;

import be.fomp.jeve.core.exceptions.JEveException;
import be.fomp.jeve.model.data.AbstractResultImpl;
import be.fomp.jeve.model.data.standard.Jumps;

public class JumpsImpl extends AbstractResultImpl implements Jumps {
	static final long serialVersionUID = 1L;
	
	private Map<Integer,Integer> jumps = new HashMap<Integer,Integer>();
	
	@SuppressWarnings("unchecked")
	public JumpsImpl(Document xmlDocument) throws JEveException{
		super(xmlDocument);
		
		Element root = xmlDocument.getRootElement();
		
		List<Element> resultSet = root.getChild("result").getChild("rowset").getChildren();
		for(Element jumpElement: resultSet){
			Integer solarSystemID = Integer.valueOf(jumpElement.getAttributeValue("solarSystemID"));
			Integer shipJumps = Integer.valueOf(jumpElement.getAttributeValue("shipJumps"));
			
			this.jumps.put(solarSystemID, shipJumps);
		}
	}
	
	public Map<Integer,Integer> getJumps()
	{
		return this.jumps;
	}
	
	public int getJumpsInSolarSystem(int SolarSystemID) {
		return getJumps().get(SolarSystemID);
	}
	
	public void writeObject(ObjectOutputStream os) {
		try {
			os.defaultWriteObject();
			os.writeObject(this.jumps);
		} catch (Exception e) { e.printStackTrace();  }
	}

	@SuppressWarnings("unchecked")
	public void readObject (ObjectInputStream is) {
		try {
			is.defaultReadObject();
			this.jumps = ((Map<Integer,Integer>)is.readObject());
		} catch (Exception e) {e.printStackTrace();}
	}
}
